package com.hjx.windowtool;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Base64;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.spi.ImageReaderSpi;
import javax.imageio.stream.FileImageInputStream;

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.helper.HttpConnection;
import org.jsoup.nodes.Document;

import com.sun.imageio.plugins.gif.GIFImageReader;
import com.sun.imageio.plugins.gif.GIFImageReaderSpi;



public class XslouTest {
	public static void main(String[] args) throws Exception {
//		System.out.println(System.getProperty("os.name"));
//		HttpConnection conn = (HttpConnection) Jsoup.connect("http://www.xslou.com/yuedu/15412/");
//		Document d = conn.get();
//		
//		System.out.println(d);
//		
//		System.out.println(conn.response().headers());
		
//		unzip();
		
//		System.out.println("dsadasd==="+System.getProperty("line.separator")+"==dsads");
		
//		System.out.println(Integer.toHexString(Color.BLACK.getRGB()));
		
		downloadImage();
		
//		readGif();
	}
	
	public static void downloadImage() throws Exception{
		HttpURLConnection conn = (HttpURLConnection) new URL("http://www.xslou.com/getimg.php?aid=67869&cid=29676538").openConnection();
		conn.setRequestProperty("Accept", "image/webp,image/apng,image/*,*/*;q=0.8");
//		conn.setRequestProperty("Accept-Encoding", "gzip, deflate");
		conn.setRequestProperty("Accept-Language", "zh-CN,zh;q=0.9");
		conn.setRequestProperty("Cache-Control", "no-cache");
		conn.setRequestProperty("Connection", "keep-alive");
		conn.setRequestProperty("Host", "www.xslou.com");
		conn.setRequestProperty("Pragma", "no-cache");
		conn.setRequestProperty("Referer", "http://www.xslou.com/shu/67869/29676538.html");
		conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36");
		conn.setRequestProperty("Cookie", "__guid=203050647.808600354136427600.1551065099891.8916; bdshare_firstime=1551065102120; UM_distinctid=16922af390a273-086b3ac7c9a04c-3c604504-1fa400-16922af390b446; __51cke__=; CNZZDATA1261943370=1606105795-1551063060-%7C1551068905; PHPSESSID=0gou1v5iamnp93iefgbjjk6av7; Hm_lvt_6fb6bee8deeec3134fac140e50fed47a=1551065103,1551073223; monitor_count=6; Hm_lpvt_6fb6bee8deeec3134fac140e50fed47a="+System.currentTimeMillis()/1000+"; __tins__19011135=%7B%22sid%22%3A%201551073222533%2C%20%22vd%22%3A%202%2C%20%22expires%22%3A%201551075033240%7D; __51laig__=2");
		conn.setDoInput(true);
		conn.setDoOutput(true);
		conn.setUseCaches(false);
		conn.setRequestMethod("GET");
		InputStream is= conn.getInputStream();
		int read=-1;
		File file = new File("E:\\1.jpg");
		FileOutputStream fos=new FileOutputStream(file);
		byte[] bs = new byte[1024];
		while((read=is.read(bs))!=-1) {
			System.out.println(new String(bs,"GBK"));
			fos.write(bs, 0, read);
			fos.flush();
		}
		
		Map<String, List<String>> headerFields = conn.getHeaderFields();
		for (String string : headerFields.keySet()) {
			System.out.println(string+"==="+headerFields.get(string));
		}
		fos.close();
		conn.disconnect();
	}

	/**
	 * @throws FileNotFoundException
	 * @throws IOException
	 */
	public static void unzip() throws FileNotFoundException, IOException {
		ZipInputStream zis=new ZipInputStream(new FileInputStream("D:\\Workspaces\\eclips_rmars\\carparkscreenapp\\target\\carparkscreenapp\\bin.zip"));
		ZipEntry zipEntry = zis.getNextEntry();
		String unzipPath="D:\\Workspaces\\eclips_rmars\\carparkscreenapp\\target\\carparkscreenapp\\111";
		if(!new File(unzipPath).exists()) {
			new File(unzipPath).mkdirs();
		}
		while((zipEntry = zis.getNextEntry())!=null) {
			System.out.println(zipEntry.getName()+"===");
			if (zipEntry.isDirectory()) {
				new File(unzipPath+File.separator+zipEntry.getName()).mkdirs();
			}else {
				 String fname=new String(unzipPath+File.separator+zipEntry.getName());
                 try{
                     //新建一个out,指向fname，fname是输出地址
                     FileOutputStream out = new FileOutputStream(fname);
                     byte[] doc = new byte[512];
                     int n;
                     //若没有读到，即读取到末尾，则返回-1
                     while((n=zis.read(doc,0,512))!=-1)
                     {
                         //这就把读取到的n个字节全部都写入到指定路径了
                         out.write(doc,0,n);
//                         System.out.println(n);
                     }
                     out.close();
                     out=null;
                     doc=null;
                 }catch (Exception ex) {
                     System.out.println("there is a problem");
                 }
			}
		}
		zis.close();
	}

	/**
	 * @throws IOException
	 * @throws FileNotFoundException
	 */
	public static void testImage() throws IOException, FileNotFoundException {
		BufferedImage image = ImageIO.read(new File("C:\\Users\\hjx\\Pictures\\Saved Pictures\\659.jpg"));
		int nomal=0x02194a;
		BufferedImage bufferedImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_BYTE_BINARY);
		for (int i = 0; i < image.getWidth(); i++) {
			for (int j = 0; j < image.getHeight(); j++) {
				int color = image.getRGB(i, j);
				bufferedImage.setRGB(i, j, color);
//				String rgb = Integer.toHexString(color);
////				if(Integer.toUnsignedLong(image.getRGB(i, j))>0xff300000) {
////					continue;
////				}
//				int red=new Color(color).getRed();
////				if(red==0||red>0x10) {
////					bufferedImage.setRGB(i, j, Color.WHITE.getRGB());
////					continue;
////				}
//				int green=new Color(color).getGreen();
//				int blue=new Color(color).getBlue();
//				if((nomal*0.3)<Integer.valueOf(red+""+green+""+blue)&&Integer.valueOf(red+""+green+""+blue)<(nomal*1.7)) {
//					
//				}else {
//					bufferedImage.setRGB(i, j, Color.WHITE.getRGB());
//					continue;
//				}
//				bufferedImage.setRGB(i, j, Color.BLACK.getRGB());
//				
//				String s="#"+Integer.toHexString(red)+Integer.toHexString(green)+Integer.toHexString(blue);
//				System.out.println(color+"==="+s);
			}
		}
		ImageIO.write(bufferedImage, "jpeg",new FileOutputStream("E:\\1111232.jpeg"));
	}
	
	public static Image readGif() throws Exception {
		GIFImageReader r=new GIFImageReader(new GIFImageReaderSpi());
		r.setInput(new FileImageInputStream(new File("e:\\1.jpg")));
		System.out.println(r.getNumImages(true));
		for (int i = 0; i < r.getNumImages(true); i++) {
			ImageIO.write(r.read(i), "jpeg", new FileOutputStream("e:\\"+(i+2)+".jpg"));
		}
		return r.read(0);
	}
	
}
